package com.guigu.controller.stock;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.guigu.pojo.design.DFile;
import com.guigu.pojo.manufacture.MDesignProcedure;
import com.guigu.pojo.stock.*;
import com.guigu.service.design.DFileService;
import com.guigu.service.stock.*;
import com.guigu.utils.CodeUtils;
import com.sun.org.apache.xpath.internal.operations.Bool;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Mr.Zhang
 * @since 2024-12-06
 */
@Controller
@RequestMapping("/s-cell")
public class SCellController {

    @Autowired
    private SCellService sCellService;

    @Autowired
    private DFileService dFileService;

    @Autowired
    private SWarehouseSettingService sWarehouseSettingService;

    @Autowired
    private SThirdProductAreaService sThirdProductAreaService;

    @Autowired
    private SFirstProductAreaService sFirstProductAreaService;

    @Autowired
    private SSecondProductAreaService sSecondProductAreaService;

    //制定安全库存配置单
    @GetMapping("/safety_stock")
    public String goSafetyStock(){
        return "jsp/stock/b/safety_stock";
    }

    //安全库存配置单查询
    @GetMapping("/Formulat_safety_stock_requery")
    public String goFormulatSafetyStockRequery(){
        return "jsp/stock/b/Formulat_safety_stock_requery";
    }

    @GetMapping("/Formulat_safety_stock")
    public String goFormulatSafetyStock(){
        return "jsp/stock/b/Formulat_safety_stock";
    }

    //安全库存配置单复核
    @GetMapping("/safetyStockReview")
    public String goFormulatSafety(){
        return "jsp/stock/b/safety_stock_review";
    }

    //安全库存配置单复核子页面
    @GetMapping("/safetyStockReviewSub")
    public String gosafetyStockReviewSub(){
        return "jsp/stock/b/safety_stock_review_sub";
    }

    @GetMapping("selectFormulate")
    @ResponseBody
    public Boolean selectFormalate(HttpServletRequest req){
        String productId = req.getParameter("productId");
        DFile dFile = dFileService.selectByProductId(productId);
        String thirdKindId = dFile.getThirdKindId();

        SThirdProductArea sThirdProductArea = sThirdProductAreaService.selectOne(thirdKindId);
        String warehouseId = sThirdProductArea.getWarehouseId();
        if(warehouseId.equals("00")){
            return false;
        }else {
            return true;
        }
    }

    @GetMapping("/initWarehouseTbl")
    @ResponseBody
    public ArrayList<SThirdProductArea> initialMaterial(HttpServletRequest req){
        Integer id = StringUtils.isNotBlank(req.getParameter("id")) ? Integer.parseInt(req.getParameter("id")) : null;
        DFile dFile = dFileService.selectById(id);
        String thirdKindId = dFile.getThirdKindId();

        ArrayList<SThirdProductArea> sThirdProductAreas = sThirdProductAreaService.selectThird(thirdKindId);

        return sThirdProductAreas;
    }

    @GetMapping("/initWarehouseTbl2")
    @ResponseBody
    public ArrayList<SThirdProductArea> initWarehouseTbl2(HttpServletRequest req){
        Integer id = StringUtils.isNotBlank(req.getParameter("id")) ? Integer.parseInt(req.getParameter("id")) : null;
        SCell sCell = sCellService.getById(id);
        String thirdKindId = sCell.getThirdKindId();

        ArrayList<SThirdProductArea> sThirdProductAreas = sThirdProductAreaService.selectThird(thirdKindId);

        return sThirdProductAreas;
    }

    @PostMapping("/insertScell")
    @ResponseBody
    public Boolean insertSCell(@RequestBody ArrayList<Object> arr){
        System.out.println(arr);
        String productId ="";
        String register = "";
        String maxAmount = "";
        String minAmount = "";
        String config = ""; //配置要求
        String maxCapacityAmount = "";
        HashMap<String, Object> map = (HashMap<String, Object>) arr.get(0);
        if (map.containsKey("productId")) {
            productId = map.get("productId").toString();
        }
        if (map.containsKey("designer")) {
            register = map.get("designer").toString();
        }
        if (map.containsKey("maxAmount")) {
            maxAmount = map.get("maxAmount").toString();
        }
        if (map.containsKey("minAmount")) {
            minAmount = map.get("minAmount").toString();
        }
        if (map.containsKey("config")) {
            config = map.get("config").toString();
        }

        DFile dFile = dFileService.selectByProductId(productId);
        dFile.setDesignCellTag("1");
        dFileService.updateById(dFile);

        SCell endSCell = sCellService.getEndSCell();

        SCell sCell = new SCell();
        CodeUtils codeUtils = new CodeUtils();
        String stockCode = codeUtils.getStockCode(endSCell);

        sCell.setStoreId(stockCode);
        sCell.setProductId(productId);
        sCell.setProductName(dFile.getProductName());
        sCell.setRegister(register);
        sCell.setRegisterTime(new Date());
        sCell.setFirstKindId(dFile.getFirstKindId());
        sCell.setFirstKindName(dFile.getFirstKindName());
        sCell.setSecondKindId(dFile.getSecondKindId());
        sCell.setSecondKindName(dFile.getSecondKindName());
        sCell.setThirdKindId(dFile.getThirdKindId());
        sCell.setThirdKindName(dFile.getThirdKindName());
        sCell.setMaxAmount(BigDecimal.valueOf(Long.parseLong(maxAmount)));
        sCell.setMinAmount(BigDecimal.valueOf(Long.parseLong(minAmount)));
        sCell.setConfig(config);
        sCell.setAmount(BigDecimal.valueOf(0L));
        HashMap<String, Object> map2 = (HashMap<String, Object>) arr.get(1);
        if (map2.containsKey("maxCapacityAmount")) {
            maxCapacityAmount = map2.get("maxCapacityAmount").toString();
        }
        sCell.setMaxCapacityAmount(BigDecimal.valueOf(Long.parseLong(maxCapacityAmount)));
        sCell.setCheckTag("0");
        boolean b = sCellService.save(sCell);
        return b;
    }

    @PostMapping("/initialProduceTable")
    @ResponseBody
    public PageInfo<SCell> initialProduceTable(@RequestBody HashMap<String, Object> map){
        PageInfo<SCell> sCellPageInfo = sCellService.selectByPage(map);
        System.out.println(sCellPageInfo);
        return sCellPageInfo;
    }

    //根据id查询库存表(未审核的库存表)
    @GetMapping("/initReviewTab")
    @ResponseBody
    public SCell initReviewTab(HttpServletRequest req){
        Integer id = StringUtils.isNotBlank(req.getParameter("id")) ? Integer.parseInt(req.getParameter("id")) : null;
        QueryWrapper<SCell> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ID", id);
        SCell sCell = sCellService.getOne(queryWrapper);

        return sCell;
    }

    //审核通过
    @GetMapping("/review")
    @ResponseBody
    public Boolean review(HttpServletRequest req){
        String storeId = req.getParameter("storeId");
        QueryWrapper<SCell> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("STORE_ID", storeId);
        SCell sCell = sCellService.getOne(queryWrapper);
        sCell.setCheckTag("1");

        boolean b = sCellService.updateById(sCell);
        return b;
    }
}

